package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.OrganizationMapper;
import cc.rengu.igas.momp.common.entity.Organization;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 机构管理表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/6/22 19:56
 */

public class OrganizationMapperImpl implements OrganizationMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCPS_POOL_NAME);

    /**
     * 使用法人机构号和机构编码级别获取总行机构编号数据库操作实现
     *
     * @param instId   法人机构号
     * @param orgLevel 机构编码级别
     * @return cc.rengu.igas.momp.common.entity.Organization
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/22 19:54
     */
    @Override
    public Organization selectHeadOfficeInfoByOrgLevel(String instId, String orgLevel) throws Exception {

        Organization organization = new Organization();
        organization.setInstId(instId);
        organization.setOrgLevel(orgLevel);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCPS_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectHeadOfficeInfoByOrgLevel");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, organization);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询法人机构号<{}>的总行机构的机构信息失败! RETURN_CODE=<{}>", instId, iReturnCode);
            return null;
        }

        return organization;
    }

    /**
     * @Author: liujinan
     * @Date: 2020/7/14
     * @Description: 查询机构信息
     * @param: instId
     * @param: orgCode
     * @Return: java.util.List<Organization>
     * @Exception:
     */
    @Override
    public Organization selectOrganization(String instId, String orgCode) throws Exception {

        String dbPoolNameOltp = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolNameOltp = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolNameOltp);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectOrganizationInfo");

        Organization organization = new Organization();
        organization.setInstId(instId);
        organization.setOrgCode(orgCode);
        organization.setOrgStatus("01");//01：正式

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, organization);
        if (0 != iReturnCode) {
            rglog.error("获取获取机构上属机构信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, organization.getOrgCode(), iReturnCode);
            return null;
        }
        return organization;
    }
}
